package com.fullstack.landscape.editor.service.impl;

import com.fullstack.landscape.editor.common.BizType;
import com.fullstack.landscape.editor.controller.vo.SpotListVO;
import com.fullstack.landscape.editor.dao.entity.SpotEntity;
import com.fullstack.landscape.editor.dao.mapper.ModuleMapper;
import com.fullstack.landscape.editor.dao.mapper.SpotMapper;
import com.fullstack.landscape.editor.service.AttachmentService;
import com.fullstack.landscape.editor.service.SpotService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * @author leven.chen
 */
@Service
public class SpotServiceImpl implements SpotService {

    /**
     * logger
     */
    private static final Logger log = getLogger(SpotServiceImpl.class);


    private final SpotMapper spotMapper;

    private final ModuleMapper moduleMapper;

    private final AttachmentService attachmentService;

    public SpotServiceImpl(SpotMapper spotMapper, ModuleMapper moduleMapper, AttachmentService attachmentService) {
        this.spotMapper = spotMapper;
        this.moduleMapper = moduleMapper;
        this.attachmentService = attachmentService;
    }

    @Override
    public List<SpotListVO> getSpotList(int moduleId) {
        List<SpotEntity> list = spotMapper.getListByModuleId(moduleId);
        return list.stream().map(item -> {
            SpotListVO vo = new SpotListVO();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
    }


    @Override
    public void saveContent(int id, String content) {
        SpotEntity entity = new SpotEntity();
        entity.setId(id);
        if (StringUtils.isBlank(content)) {
            entity.setContent("");
        } else {
            String newContent = this.processContent(content);
            entity.setContent(newContent);
        }

        int n = spotMapper.updateSelectiveById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(int moduleId, String name, String avatarPath) {
        SpotEntity entity = new SpotEntity();
        entity.setModuleId(moduleId);
        entity.setName(name);

        if (StringUtils.isNoneBlank(avatarPath)) {
            entity.setAvatarPath(avatarPath);
        }
        spotMapper.insert(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBasicInfo(int id, String name, String avatarPath) {
        SpotEntity entity = new SpotEntity();
        entity.setId(id);
        entity.setName(name);

        if (StringUtils.isNoneBlank(avatarPath)) {
            entity.setAvatarPath(avatarPath);
        }
        spotMapper.updateSelectiveById(entity);
    }

    @Override
    public void uploadVideo(int id, String videoPath) {
        if (StringUtils.isBlank(videoPath)) {
            throw new RuntimeException("文件不能为空");
        }
        SpotEntity entity = new SpotEntity();
        entity.setId(id);
        entity.setVideoPath(videoPath);
        spotMapper.updateSelectiveById(entity);
    }

    @Override
    public void delete(int id) {
        spotMapper.delete(id);
    }

    @Override
    public String uploadImage(int id, MultipartFile file) {
        if (Objects.isNull(file)) {
            throw new RuntimeException("文件不能为空");
        }
        return attachmentService.save(file);
    }

    @Override
    public String uploadImage(int id, String originalFilename, File file) {
        return attachmentService.save(originalFilename, file);
    }

    @Override
    public void removeVideo(int id, String type) {
        switch (type) {
            case BizType.MODULE:
                break;
            case BizType.SPOT:
                /*Optional<SpotEntity> optional = spotMapper.getById(id);
                optional.ifPresent(entity -> {
                    String videoPath = entity.getVideoPath();
                    attachmentService.removeAttachment(videoPath);
                });*/
                spotMapper.removeVidePath(id);
                break;
            default:
                throw new RuntimeException("不支持的业务类型");
        }
    }

    private String processContent(String content) {
        log.debug("content={}", content);
        return content;
    }
}
